Component({
  properties: {
    // 如果需要从外部传递数据，可以在这里定义组件的属性
  },
  data: {
    userInfo: {
      gender: 1  // 测试数据，1表示已绑定，2表示未绑定
    },
    icons: {
      yibangding: "cloud://tfjkp-0ge80vu9ea710148.7466-tfjkp-0ge80vu9ea710148-1305195523/myjkp/myProfilePage/yibangding.png",
      weibangding: "cloud://tfjkp-0ge80vu9ea710148.7466-tfjkp-0ge80vu9ea710148-1305195523/myjkp/myProfilePage/weibangding.png",
    },
    iconLists: [],
    showBindingModal: false,
    bindingName: '',
    bindingStudentId: '',
    isEditing: false,
    showModal: false,
    name: '',
    studentId: '',
    formData: {
      name: '',
      studentId: ''
    },
    queryResult: null
  },
  lifetimes: {
    created() {
      console.log('MyProfile 组件已创建');
    },
    attached() {
      console.log('attached组件已挂载到页面');
      const userInfo = wx.getStorageSync('userInfo') || {}; 
      const icons = {
        logo: "cloud://tfjkp-0ge80vu9ea710148.7466-tfjkp-0ge80vu9ea710148-1305195523/myjkp/myProfilePage/mianyanglogo.png",
        renzheng: "cloud://tfjkp-0ge80vu9ea710148.7466-tfjkp-0ge80vu9ea710148-1305195523/myjkp/myProfilePage/yirenzheng.png",
        yibangding: "cloud://tfjkp-0ge80vu9ea710148.7466-tfjkp-0ge80vu9ea710148-1305195523/myjkp/myProfilePage/yibangding.png",
        weibangding: "cloud://tfjkp-0ge80vu9ea710148.7466-tfjkp-0ge80vu9ea710148-1305195523/myjkp/myProfilePage/weibangding.png",
        personalcenter: "cloud://tfjkp-0ge80vu9ea710148.7466-tfjkp-0ge80vu9ea710148-1305195523/myjkp/myProfilePage/editInfo.png",
      }
      const iconLists = [
        {
          icon: 'cloud://tfjkp-0ge80vu9ea710148.7466-tfjkp-0ge80vu9ea710148-1305195523/myjkp/myProfilePage/icon/jibenxinxi.png',
          name: '基本信息',
          url: '/pages/my-profile/basic-info/basic-info',
          isenabled: true, // 页面是否启用
        },
        {
          icon: 'cloud://tfjkp-0ge80vu9ea710148.7466-tfjkp-0ge80vu9ea710148-1305195523/myjkp/myProfilePage/icon/duihuanjilu.png',
          name: '兑换记录',
          url: '/pages/my-profile/exchange-records/exchange-records', // 没有 URL，表示该项正在开发
          isenabled: true
        },
        {
          icon: 'cloud://tfjkp-0ge80vu9ea710148.7466-tfjkp-0ge80vu9ea710148-1305195523/myjkp/myProfilePage/icon/yundongmubiao.png',
          name: '运动目标',
          url: '/pages/my-profile/moving-target/moving-target', // 没有 URL，表示该项正在开发
          isenabled: true
        },
        {
          icon: 'cloud://tfjkp-0ge80vu9ea710148.7466-tfjkp-0ge80vu9ea710148-1305195523/myjkp/myProfilePage/icon/xiugaixinxi.png',
          name: '修改信息',
          url: '/pages/my-profile/edit-basicinfo/edit-basicinfo',
          isenabled: true, // 页面启用
        },
        {
          icon: 'cloud://tfjkp-0ge80vu9ea710148.7466-tfjkp-0ge80vu9ea710148-1305195523/myjkp/myProfilePage/icon/kefuzhongxin.png',
          name: '客服中心',
          url: '', // 没有 URL，表示该项正在开发
          isenabled: false // 不可用
        },
        {
          icon: 'cloud://tfjkp-0ge80vu9ea710148.7466-tfjkp-0ge80vu9ea710148-1305195523/myjkp/myProfilePage/icon/canshushezhi.png',
          name: '参数设置',
          url: '/pages/my-profile/parameter/parameter', // 没有 URL，表示该项正在开发
          isenabled: true // 不可用
        }
        // ,
        // {
        //   icon: 'cloud://tfjkp-0ge80vu9ea710148.7466-tfjkp-0ge80vu9ea710148-1305195523/myjkp/myProfilePage/icon/tuichuxitong.png',
        //   name: '退出系统',
        //   url: '', // 没有 URL，表示该项正在开发
        //   isenabled: false // 不可用
        // }
      ]
      this.setData({
        userInfo: userInfo,
        icons: icons,
        iconLists: iconLists
      });
      wx.hideLoading()
    },
    ready() {
      console.log('MyProfile 组件加载完成，数据已准备就绪');
      console.log('MyProfile 组件加载完成，准备调用 customMethod');
      this.customMethod(); // 在组件加载完成后调用
    },
    detached() {
      console.log('MyProfile 组件已从页面移除');
    }
  },
  methods: {
    navigateToPage: function (e) {
      const {
        url,
        name,
        isenabled = false, // 默认未启用
        title
      } = e.currentTarget.dataset;
      if (isenabled) {
        if (url) {
          wx.navigateTo({
            url: `${url}`,
            success: function () {
              console.log('导航成功：', name);
            },
            fail: function (err) {
              console.error('导航失败：', name, err);
            }
          });
        } else {
          wx.showToast({
            title: `${name} 功能开发中~`,
            icon: 'none',
            duration: 2000
          });
        }
      } else {
        wx.showToast({
          title: `${name}开发中，敬请期待~`,
          icon: 'none',
          duration: 2000
        });
      }
    },

    toRun: function () {
      wx.navigateTo({
        url: '/pages/my-profile/personal-center/personal-center',
        success: function () {
          console.log('跳转到个人中心成功');
        },
        fail: function (err) {
          console.error('跳转到个人中心失败:', err);
        }
      });
    },
    // 暴露给页面调用的方法
    customMethod: function () {
      console.log('MyProfile 组件方法 customMethod 被调用');
    },
    onLoad: function() {
      console.log('页面加载时的数据：', this.data);
    },
    // 显示绑定表单
    showBindingForm() {
      this.setData({
        showBindingModal: true,
        queryResult: null,
        formData: {
          name: '',
          studentId: ''
        }
      });
    },
    // 隐藏绑定表单
    hideBindingForm() {
      this.setData({
        showBindingModal: false
      });
    },
    
    // 查询信息
    async queryInfo() {
      const { name, studentId } = this.data.formData;
      console.log("姓名：" + name + " 学号：" + studentId); 
      if (!name || !studentId) {
        wx.showToast({
          title: '请填写完整信息',
          icon: 'none'
        });
        return;
      }

      try {
        // 这里替换成你的实际接口调用
        const res = await wx.cloud.callFunction({
          name: 'queryStudentInfo',
          data: {
            name,
            studentId
          }
        });

        if (res.result && res.result.data) {
          this.setData({
            queryResult: res.result.data
          });
        } else {
          wx.showToast({
            title: '未找到相关信息，请重新输入',
            icon: 'none'
          });
        }
      } catch (error) {
        wx.showToast({
          title: '查询失败，请重试',
          icon: 'none'
        });
      }
    },
    // 确认绑定
    confirmBinding() {
      const { name, studentId } = this.data.queryResult;
      wx.showModal({
        title: '确认绑定',
        content: `请确认姓名：${name}，学号：${studentId} 是否是你本人？`,
        success: async (res) => {
          if (res.confirm) {
            try {
              // 这里替换成你的实际绑定接口
              await wx.cloud.callFunction({
                name: 'bindStudentInfo',
                data: {
                  name,
                  studentId
                }
              });
              
              wx.showToast({
                title: '绑定成功',
                icon: 'success'
              });
              
              this.setData({
                showBindingModal: false
              });
              
              // 刷新用户信息
              this.getUserInfo();
            } catch (error) {
              wx.showToast({
                title: '绑定失败，请重试',
                icon: 'none'
              });
            }
          }
        }
      });
    },
  }
});